package com.minshenglife.zanzan.web.weixin.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.minshenglife.commons.security.utils.AccessTokenUtils;
import com.minshenglife.commons.security.utils.pp.UserIdentify;
import com.minshenglife.commons.security.utils.pp.UserIdentify.UserType;
import com.minshenglife.zanzan.entity.EMOUserGroup;
import com.minshenglife.zanzan.pub.message.MessageUtil;
import com.minshenglife.zanzan.pub.message.resp.BaseMessage;
import com.minshenglife.zanzan.pub.message.resp.NewsMessage;
import com.minshenglife.zanzan.pub.message.resp.TextMessage;
import com.minshenglife.zanzan.pub.message.resp.model.Article;
import com.minshenglife.zanzan.pub.utils.WXKeys;
import com.minshenglife.zanzan.service.AccessTokenService;
import com.minshenglife.zanzan.service.EMOUserGroupService;

/**
 * chuli菜单事件
 * @author jianghaibo
 *
 */
@Component
public class EMOWXMenuEventService {
	
	Logger log = LoggerFactory.getLogger(EMOWXMenuEventService.class);
	
	@Resource
	EMOUserGroupService emoUserGroupService;
	@Resource
	AccessTokenService accessTokenService;
	
	/**
	 * 处理菜单事件
	 * @param key 事件key值
	 * @param access_token 事件请求者的微信号access_token
	 * @param baseUrl 
	 * @return
	 */
	public String getEventResult(String key,String fromUser,String toUser) {
		String repMsg = "";
		//校验请求者是否已经绑定员工号 
		EMOUserGroup userGroup=emoUserGroupService.getUserByOpenid(fromUser);
		
		String access_token=null;
		List<Article> list=new ArrayList<Article>();
		//测试
//		userGroup = new EMOUserGroup();
//		userGroup.setOpenid("olcc4t9mKMKjV--zMCvfS5qG-52s");
		if(userGroup != null){
			access_token=AccessTokenUtils.newToken(new UserIdentify(UserType.weixin,fromUser), userGroup.getOpenid());
			accessTokenService.insertAccessToken(userGroup,access_token);//插入页面时效
			if(WXKeys.WX_EMO_BTN_MENU_SPEAK.equals(key)){
				NewsMessage newsMessage=new NewsMessage(fromUser,toUser,new Date().getTime(),
						MessageUtil.RESP_MESSAGE_TYPE_NEWS,0,0,null); 
				list.add(new Article("黄敏在民生保险第九届高峰会上的致辞",null,WXKeys.WX_BASEURL+"/images/weixin/zhici.jpg",WXKeys.WX_BASEURL+"/jsp/weixin/zhici.jsp?access_token="+access_token));
				list.add(new Article("黄敏在“精英三千”第三期培训上的讲话",null,WXKeys.WX_BASEURL+"/images/weixin/speak.jpg",WXKeys.WX_BASEURL+"/jsp/weixin/speak.jsp?access_token="+access_token));
				newsMessage.setArticles(list);
				newsMessage.setArticleCount(list.size());
				repMsg = MessageUtil.newsMessageToXml(newsMessage);
			}else if(WXKeys.WX_EMO_BTN_MENU_DYNAMIC.equals(key)){
				String content = "4月23日，黄总参加公司第九届高峰会并致词";
				TextMessage textMessage = new TextMessage(fromUser,toUser,new Date().getTime(),MessageUtil.RESP_MESSAGE_TYPE_TEXT,0,content); 
				repMsg = MessageUtil.textMessageToXml(textMessage);
			}else if(WXKeys.WX_EMO_BTN_MENU_EMAIL.equals(key)){
				String content = "你好，请将您在工作中遇到的问题告诉我";
				TextMessage textMessage = new TextMessage(fromUser,toUser,new Date().getTime(),MessageUtil.RESP_MESSAGE_TYPE_TEXT,0,content); 
				repMsg = MessageUtil.textMessageToXml(textMessage);
			}else if(WXKeys.WX_EMO_BTN_MENU_SUGGEST.equals(key)){
				String content = "对于民生保险，欢迎您提出宝贵建议。";
				TextMessage textMessage = new TextMessage(fromUser,toUser,new Date().getTime(),MessageUtil.RESP_MESSAGE_TYPE_TEXT,0,content); 
				repMsg = MessageUtil.textMessageToXml(textMessage);
			}else if(WXKeys.WX_EMO_BTN_MENU_FOREFRONT.equals(key)){
				StringBuffer sb = new StringBuffer();
				sb.append("保监会开通“保监微课堂”消费者教育微信公众平台2014年09月01日　　9月1日，保监会消费者权益保护局开通了以保险消费者教育为核心定位的微信公众平台“保监微课堂”，将信息推送与对话服务相结合，进一步完善保监会的消费者教育网络，与网站专题、热线电话、普及读物、报纸专栏、教育微博共同形成六位一体的保险消费者教育格局。");
				sb.append("“保监微课堂”微信公众平台设置“知识文库”、“聚焦话题”、“晓保窗口”三个板块，以图文结合形式帮助消费者了解保险相关知识和进行消费风险提示，树立正确的保险消费理念。“知识文库”系统整合保险知识条目，采用多图文样式，便于消费者结合自身需要进行自助查询。“聚焦话题”为消费者提供保险热点事件的保险知识解读以及投保理赔案例分析，发布消费风险提示。“晓保窗口”依托定期举办保险知识问答、有奖参与等活动，拉近监管机关与消费者之间的距离，并提供12378投诉维权热线的快速入口，方便消费者投诉维权。");
				sb.append("“保监微课堂”以较为生活化的视角向消费者传递信息，有助于消费者理解保险知识，将推动全社会形成“学保险、懂保险、用保险”的氛围。来源:保监会");
				String content = sb.toString();

				TextMessage textMessage = new TextMessage(fromUser,toUser,new Date().getTime(),MessageUtil.RESP_MESSAGE_TYPE_TEXT,0,content); 
				repMsg = MessageUtil.textMessageToXml(textMessage);
			}
		}else{
			//用时间串生成access_token
			access_token=AccessTokenUtils.newToken(new UserIdentify(UserType.weixin,fromUser), String.valueOf(new Date().getTime()));
			accessTokenService.insertAccessTokenOfNotUser(fromUser,access_token);//插入页面时效
			list.add(new Article("查看详细信息请绑定员工号","点击该图文消息进入绑定页面",null,WXKeys.WX_BASEURL+"/jsp/weixin/emo/register.jsp?access_token="+access_token));//"/user/gotoRegister.do?access_token="+access_token
			NewsMessage newsMessage=new NewsMessage(fromUser,toUser,new Date().getTime(),MessageUtil.RESP_MESSAGE_TYPE_NEWS,0,0,null); 
			newsMessage.setArticles(list);
			newsMessage.setArticleCount(list.size());
			repMsg = MessageUtil.newsMessageToXml(newsMessage);
		}
		return repMsg;
	}
	
}
